package cn.lxh.basic.controller;

import cn.lxh.basic.dto.LoginDto;
import cn.lxh.basic.dto.WechatDto;
import cn.lxh.basic.exception.BusinessException;
import cn.lxh.org.basic.utils.AjaxResult;
import cn.lxh.user.service.impl.LogininfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.Map;

/**
 * 后台登录点击功能按 钮
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LogininfoServiceImpl loginService;
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try {
            //登录成功之后，service返回token  + logininfo
            Map<String, Object> map = loginService.account(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatDto dto){
        try {
            //登录成功之后，service返回token  + logininfo
            return loginService.wechat(dto);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WechatDto dto){
        try {
            //登录成功之后，service返回token  + logininfo
            Map<String, Object> map = loginService.binder(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    @GetMapping("lxh")
    public String wechat(){
        return "测试";
    }



}
